package me.youline.dataServer.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import me.youline.dataServer.entity.socialActionMsg.SocialActionMsg;
import me.youline.dataServer.entity.socialActionMsg.SocialActionMsg.Action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Component;

/**
 * 社交事件消息DAO
 * @author linhan
 *
 */
@Component
public class SocialActionMsgDAO {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private static final String TABLE_NAME = "yline_social_notice_msg";

	private static final String QUERY_LIST_SQL = " select * from " + TABLE_NAME
			+ " where id in(:ids)";
	
	private static final String QUERY_SQL = "select * from " + TABLE_NAME + " where id = ?";
	
	private static final String QUERY_SQL_BY_TYPE_AND_TARGET = " selecct id from " + TABLE_NAME + " where  action= ? and targetId = ? ";
	
	private SimpleJdbcInsert insertOperation;

	@PostConstruct
	private void init() {
		insertOperation = new SimpleJdbcInsert(jdbcTemplate)
				.withTableName(TABLE_NAME)
				.usingGeneratedKeyColumns("id");
	}

	private RowMapper<SocialActionMsg> ROW_MAPPER = new RowMapper<SocialActionMsg>() {

		@Override
		public SocialActionMsg mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			SocialActionMsg msg = new SocialActionMsg();
			msg.setAction(Action.values()[rs.getInt("action")]);
			msg.setInitiator(rs.getLong("initiator"));
			msg.setTargetId(rs.getLong("targetId"));
			msg.setId(rs.getLong("id"));
			msg.setAttach(rs.getString("attach"));
			msg.setCreateTime(rs.getTimestamp("createTime"));
			return msg;
		}
	};

	/**
	 * 批量获取社交通知消息
	 * 
	 * @param userId
	 *            通知用户ID
	 * @param page
	 *            分页对象
	 * @return
	 */
	public List<SocialActionMsg> getSocailNotifications(Collection<Long> ids) {
		if(ids == null || ids.isEmpty()){
			return Collections.emptyList();
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("ids" ,ids);
		return namedParameterJdbcTemplate.query(QUERY_LIST_SQL, map,ROW_MAPPER);
	}

	/**
	 * 添加社交事件消息
	 * @param msg
	 * @return
	 */
	public Long addSocialActionNotifyMsg(final SocialActionMsg msg){ 
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		map.put("initiator",msg.getInitiator());
		map.put("action",msg.getAction().ordinal());
		map.put("targetId",msg.getTargetId());
		map.put("createTime", msg.getCreateTime());
		map.put("attach", msg.getAttach());
		return insertOperation.executeAndReturnKey(map).longValue();
	}
	
	/**
	 * 根据id获取社交事件消息
	 * @param id
	 * @return
	 */
	public SocialActionMsg getSocialActionMsgById(Long id){
		try {
			return jdbcTemplate.queryForObject(QUERY_SQL , new Object[]{id}, ROW_MAPPER);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	public List<Long> getMsgIdsByActionAndTarget(Action action, Long targetId){
		return jdbcTemplate.queryForList(QUERY_SQL_BY_TYPE_AND_TARGET, new Object[]{action,targetId},Long.class);
	}
}
